
/*
* 平行结构转化树，需先将defaultTree赋值，如果不赋值，则默认平行数组中标识字段为id，名称为name父id为pid
* */
export default {
  data() {
    return {
      /*平行结构数据*/
      /*oldList:[],
      /!*转化后的树*!/
      newTree:[],
      /!*平行数据的属性，根据他来判断父子结构参数*!/
      defaultTree:{
        id:'id',
        label:'name',
        pid:"pid",
        children:"children"
      },
      /!*需要转化为平行结构的树*!/
      oldTree:[],
      /!*由树转化为平行数组*!/
      newList:[],*/
    }
  },
  created() {
    /*数组转树*/
    // this.getNewTree();
    /*树转数组*/
    // this.getNewList(this.oldTree);
  },
  mounted() {},
  methods: {
    /*分配新的空间，防止更改被复制元素*/
    deepClone(obj){
      let _obj = JSON.stringify(obj),
          objClone = JSON.parse(_obj);
      return objClone;
    },
    /*根据子节点查询父节点*/
    getTreeRelationPData(allList, leafList,{idKey,pIdKey}) {
      const resultList = []
      leafList.forEach(leaf => {
        resultList.push(leaf)
        this._getPFromList(leaf,allList,resultList, {idKey,pIdKey})
      })
      return resultList
    },
    _getPFromList(leaf,treeList,resultList,{idKey,pIdKey}) {
      idKey = idKey || 'id'
      pIdKey = pIdKey || 'pid'
      if(resultList.find(item => item[idKey] === leaf[pIdKey])){
        return
      }
      treeList.forEach(node => {
        if(node[idKey] === leaf[pIdKey]) {
          resultList.push(node)
          if(node[pIdKey] && node[pIdKey] !== node[idKey]) {
            this._getPFromList(node,treeList,resultList,{idKey,pIdKey})
          }
        }
      })
    },
    /*平行数据转树*/
    getNewTree(oldList,Treedefault,newTree,showChildren){
      /*所有父级节点*/
      let pList=[]
      /*遍历所有父级节点*/
      oldList.forEach(item=>{
        /*查询改节点是否在数组中有父级*/
        let flag=oldList.some((n)=>n[Treedefault.id]==item[Treedefault.pid]);
        /*该节点在数组中没有父级，则放入父级数据中*/
        if(!flag){
          pList.push(item);
        }
      });
      /*给父节点赋children*/
      pList.forEach(item=>{
        let list = [];
        list = this.getTreeChildren(item[Treedefault.id],oldList,Treedefault,showChildren);
        if(!(list.length==0 && showChildren)){
          this.$set(item,'children',list)
        }
      });
      /*整理好的树赋值给树*/
      newTree=pList;
      return newTree
    },
    /*递归出子节点数据*/
    getTreeChildren(pid,oldList,Treedefault,showChildren){
      let list=[];
      oldList.forEach(item=>{
        if(item[Treedefault.pid]==pid){
          let copyList=this.deepClone(item);
          let children = [];
          children = this.getTreeChildren(item[Treedefault.id],oldList,Treedefault,showChildren);
          if(!(children.length==0 && showChildren)){
            copyList.children=children;
          }
          list.push(copyList)
        }
      })
      return list;
    },
    /*树转平行数据*/
    getNewList(tree,newList){
      tree.forEach(item=>{
        let obj=this.deepClone(item);
        if(item.children && item.children.length>0){
          /*删除子节点*/
          delete obj['children'];
          newList.push(obj);
          this.getNewList(item.children,newList)
        }else{
          newList.push(obj);
        }
      })
    },
  },
  watch:{
    /*oldList:function(list){
      console.log('list',list)
      this.getNewTree();
    },
    oldTree:function(tree){
      this.newList=[];
      this.getNewList(tree);
    },*/
  },
}
